package com.iknow.ui.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.iknow.IKnow;
import com.iknow.image.ProfileImageCacheCallback;
import com.iknow.image.ProfileImageCacheManager;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import com.iknow.xmpp.service.AudioMessage;
import com.iknow.xmpp.service.ImageMessage;
import com.iknow.xmpp.service.Message;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.List;

public class XmppMessageAdapter extends BaseAdapter
{
  public static SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
  private ProfileImageCacheCallback callback;
  private Context mContext;
  private LayoutInflater mInflater = null;
  private List<Message> mListMessages;
  private String mUserJid;
  protected MediaPlayer mediaPlayer;
  private View.OnClickListener playClickListener;

  public XmppMessageAdapter(Context paramContext, LayoutInflater paramLayoutInflater)
  {
    this.callback = new ProfileImageCacheCallback(this)
    {
      public void refresh(, Bitmap paramBitmap)
      {
        this.this$0.notifyDataSetChanged();
      }
    };
    this.playClickListener = new View.OnClickListener(this)
    {
      private boolean mbPlaying = false;

      public void onClick()
      {
        paramView.setBackgroundResource(2130903079);
        paramView.startAnimation(AnimationUtils.loadAnimation(paramView.getContext(), 2130968578));
      }
    };
    this.mInflater = paramLayoutInflater;
    this.mContext = paramContext;
  }

  private void fillAudioMsgDataToView(AudioMessage paramAudioMessage, View paramView)
  {
    AudioMsgHolder localAudioMsgHolder = (AudioMsgHolder)paramView.getTag();
    localAudioMsgHolder.btnPlay.setTag(paramAudioMessage.getFileName());
    localAudioMsgHolder.btnPlay.setOnClickListener(this.playClickListener);
    boolean bool = StringUtil.compareTime(paramAudioMessage.getTimestamp());
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    if (bool)
      localAudioMsgHolder.time.setText(sdf2.format(paramAudioMessage.getTimestamp()));
    while (true)
    {
      return;
      localAudioMsgHolder.time.setText(localSimpleDateFormat.format(paramAudioMessage.getTimestamp()));
    }
  }

  private String fillDownloadUrl(Message paramMessage)
  {
    return null;
  }

  private void fillImageMsgDataToView(ImageMessage paramImageMessage, View paramView, boolean paramBoolean)
  {
    SimpleDateFormat localSimpleDateFormat;
    ImageMsgHolder localImageMsgHolder = (ImageMsgHolder)paramView.getTag();
    if (!(paramBoolean))
    {
      localImageMsgHolder.image.setImageBitmap(IKnow.mProfileImageCacheManager.getContentBitmap(fillDownloadUrl(paramImageMessage), this.callback));
      boolean bool = StringUtil.compareTime(paramImageMessage.getTimestamp());
      localSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
      if (!(bool))
        break label101;
      localImageMsgHolder.time.setText(sdf2.format(paramImageMessage.getTimestamp()));
    }
    while (true)
    {
      while (true)
      {
        return;
        localImageMsgHolder.image.setImageBitmap(IKnow.mProfileImageCacheManager.getFromLocal(paramImageMessage.getThumbnail()));
      }
      label101: localImageMsgHolder.time.setText(localSimpleDateFormat.format(paramImageMessage.getTimestamp()));
    }
  }

  private void fillTextMsgDataToView(Message paramMessage, View paramView)
  {
    Object localObject = paramView.getTag();
    boolean bool = StringUtil.compareTime(paramMessage.getTimestamp());
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    TextMsgHolder localTextMsgHolder = (TextMsgHolder)localObject;
    if (bool)
      localTextMsgHolder.time.setText(sdf2.format(paramMessage.getTimestamp()));
    while (true)
    {
      localTextMsgHolder.message.setText(paramMessage.getBody());
      return;
      localTextMsgHolder.time.setText(localSimpleDateFormat.format(paramMessage.getTimestamp()));
    }
  }

  private View newAudioMsgView(boolean paramBoolean)
  {
    View localView;
    AudioMsgHolder localAudioMsgHolder = new AudioMsgHolder(this, null);
    if (paramBoolean)
      localView = this.mInflater.inflate(2130903087, null);
    while (true)
    {
      localAudioMsgHolder.btnPlay = ((Button)localView.findViewById(2131296425));
      localAudioMsgHolder.progressBar = ((ProgressBar)localView.findViewById(2131296426));
      localAudioMsgHolder.time = ((TextView)localView.findViewById(2131296288));
      localView.setTag(localAudioMsgHolder);
      return localView;
      localView = this.mInflater.inflate(2130903084, null);
    }
  }

  private View newImageView(boolean paramBoolean)
  {
    View localView;
    ImageMsgHolder localImageMsgHolder = new ImageMsgHolder(this, null);
    if (paramBoolean)
      localView = this.mInflater.inflate(2130903088, null);
    while (true)
    {
      localImageMsgHolder.image = ((ImageView)localView.findViewById(2131296432));
      localImageMsgHolder.progressBar = ((ProgressBar)localView.findViewById(2131296426));
      localImageMsgHolder.time = ((TextView)localView.findViewById(2131296288));
      localView.setTag(localImageMsgHolder);
      return localView;
      localView = this.mInflater.inflate(2130903085, null);
    }
  }

  private void playAudio(String paramString)
  {
    if (this.mediaPlayer == null)
    {
      this.mediaPlayer = new MediaPlayer();
      this.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(this)
      {
        public void onCompletion()
        {
          paramMediaPlayer.release();
        }
      });
    }
    if (new File(paramString).exists());
    try
    {
      FileInputStream localFileInputStream = new FileInputStream(paramString);
      this.mediaPlayer.reset();
      this.mediaPlayer.setDataSource(localFileInputStream.getFD());
      this.mediaPlayer.prepare();
      this.mediaPlayer.start();
      return;
    }
    catch (Exception localException)
    {
      Loger.e("playAudio", "play audio error!!");
      localException.printStackTrace();
    }
  }

  public void addMessageText(Message paramMessage)
  {
    this.mListMessages.add(paramMessage);
  }

  public int getCount()
  {
    return this.mListMessages.size();
  }

  public Message getItem(int paramInt)
  {
    return ((Message)this.mListMessages.get(paramInt));
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    View localView;
    Message localMessage = (Message)this.mListMessages.get(paramInt);
    boolean bool = false;
    if (localMessage.getFrom().equalsIgnoreCase(this.mUserJid))
      bool = true;
    if (localMessage instanceof ImageMessage)
    {
      localView = newImageView(bool);
      fillImageMsgDataToView((ImageMessage)localMessage, localView, bool);
    }
    while (true)
    {
      return localView;
      if (!(localMessage instanceof AudioMessage))
        break;
      localView = newAudioMsgView(bool);
      fillAudioMsgDataToView((AudioMessage)localMessage, localView);
    }
    if (bool)
      localView = newUserTextMsgView();
    while (true)
    {
      while (true)
        fillTextMsgDataToView(localMessage, localView);
      localView = newFriendTextMsgView();
    }
  }

  public View newFriendTextMsgView()
  {
    TextMsgHolder localTextMsgHolder = new TextMsgHolder(this);
    View localView = this.mInflater.inflate(2130903086, null);
    localTextMsgHolder.message = ((TextView)localView.findViewById(2131296433));
    localTextMsgHolder.time = ((TextView)localView.findViewById(2131296288));
    localView.setTag(localTextMsgHolder);
    return localView;
  }

  public View newUserTextMsgView()
  {
    TextMsgHolder localTextMsgHolder = new TextMsgHolder(this);
    View localView = this.mInflater.inflate(2130903089, null);
    localTextMsgHolder.message = ((TextView)localView.findViewById(2131296433));
    localTextMsgHolder.time = ((TextView)localView.findViewById(2131296288));
    localView.setTag(localTextMsgHolder);
    return localView;
  }

  public void onPause()
  {
    if (this.mediaPlayer != null)
    {
      if (this.mediaPlayer.isPlaying())
        this.mediaPlayer.stop();
      this.mediaPlayer.release();
      this.mediaPlayer = null;
    }
  }

  public void setMessageList(List<Message> paramList)
  {
    this.mListMessages = paramList;
  }

  public void setUserJid(String paramString)
  {
    this.mUserJid = paramString;
  }

  private class AudioMsgHolder
  {
    Button btnPlay;
    ProgressBar progressBar;
    TextView time;
  }

  private class ImageMsgHolder
  {
    ImageView image;
    ProgressBar progressBar;
    TextView time;
  }

  public class TextMsgHolder
  {
    TextView message;
    TextView time;
  }
}